﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Quaack.Data;
using Quaack.Web.Controls;

namespace Quaack.Web
{
    public partial class SearchResult : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            Provider.CurrentUserPage = null;

            string query = Request.QueryString["q"];
            Master.TxtZoeken.Text = query;
            Master.TxtZoeken.CssClass = "results";

            var result = Profiel.Zoeken(query);

            if (result == null)
                return;

            if (result.Count == 1)
            {
                // Direct naar deze pagina gaan
                Response.Redirect(Page.ResolveUrl("~/GebruikerHome.aspx?profiel=" + result[0].Id));
            }

            SearchResultTable.Rows.Clear();

            int columnCount = 0;
            TableRow rowToAdd = new TableRow();

            foreach (var profile in result)
            {
                columnCount = columnCount + 1;

                var dataholder = (CombinedContentHolder)Page.LoadControl("~/Controls/CombinedContentHolder.ascx");
                dataholder.Profiel = profile;
                dataholder.ShowContent = true;

                var cell = new TableCell();
                cell.Controls.Add(dataholder);

                rowToAdd.Cells.Add(cell);

                if (columnCount % 2 == 0 || columnCount == result.Count)
                {
                    SearchResultTable.Rows.Add(rowToAdd);

                    rowToAdd = new TableRow();
                }
            }
        }
    }
}